package com.ocom.xcxapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.HttpUtils;
import com.ocom.common.constant.AliPayServiceUrl;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.entity.master.EntityCompanyList;
import com.ocom.common.entity.master.EntityWechatAppInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.company.GetXcxComListResponse;
import com.ocom.common.request.company.GetXcxListOfComRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.human.XfUserInfoVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.xcxapi.client.CompanyClientService;
import com.ocom.xcxapi.client.HumanClientService;
import com.ocom.xcxapi.client.PayConfigClientService;
import com.ocom.xcxapi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
//@PreAuthorize("hasRole('ROLE_ALIXCX')")
public class AliPayNotAuthController {

    @Autowired
    private HumanClientService humanClientService;
    @Autowired
    private PayConfigClientService payConfigClientService;
    @Autowired
    private CompanyClientService companyClientService;

//    private static final String PAY_URL = "http://192.168.11.249:8080/ocom_pay";
    private static final String PAY_URL = "http://192.168.3.123:32880/ocom_pay";//TODO 上线时需要更换为生产环境统一支付地址


    @PostMapping(AliPayServiceUrl.URL_getSignCom)
    public Result getSignCom(@RequestBody GetSignComRequest request) {
        GetXcxListOfComRequest getXcxListOfComRequest = new GetXcxListOfComRequest();
        getXcxListOfComRequest.setAppid(request.getAppid());
        Result result = companyClientService.getXcxListOfCom(getXcxListOfComRequest);
        List<GetXcxComListResponse> getXcxComListResponse = new ArrayList<>();
        if (200 == result.getCode()) {
            List<EntityWechatAppInfo> wchatAppInfoList = JSON.parseArray(JSON.toJSONString(result.getData()), EntityWechatAppInfo.class);
            List<Long> comIds = wchatAppInfoList.stream().map(EntityWechatAppInfo -> EntityWechatAppInfo.getComId()).collect(Collectors.toList());

            result = companyClientService.getXcxComList(comIds);
            if (200 == result.getCode()) {
                List<EntityCompanyList> companyLists = JSON.parseArray(JSON.toJSONString(result.getData()), EntityCompanyList.class);
                getXcxComListResponse = companyLists.stream().map(data -> {
                    return new GetXcxComListResponse(data.getComId(), data.getComName());
                }).collect(Collectors.toList());

            }
        }
        return ResultUtil.success(getXcxComListResponse);
    }

    @PostMapping(AliPayServiceUrl.URL_selectSignCom)
    public Result selectSignCom(@RequestBody SelectSignComRequest request) {

        EntityXfMerchPayInfo entityXfMerchPayInfo = new EntityXfMerchPayInfo();
        entityXfMerchPayInfo.setComId(request.getComId());
        entityXfMerchPayInfo.setMerchId(0);

        Result getPayInfoResult = payConfigClientService.getPayInfo(entityXfMerchPayInfo);
        Map<String, Object> responseMap = new HashMap<>();
        if (200 == getPayInfoResult.getCode()) {
            EntityXfMerchPayInfo resultEntityXfMerchPayInfo = JSON.parseObject(JSON.toJSONString(getPayInfoResult.getData()), EntityXfMerchPayInfo.class);
            Map<String, Object> retMap = new HashMap<>();
            retMap.put("companyId", resultEntityXfMerchPayInfo.getCompanyId());
            retMap.put("userName", resultEntityXfMerchPayInfo.getUserName());
            retMap.put("password", resultEntityXfMerchPayInfo.getPassword());
            retMap.put("bizCode", "SCHOOL_PAYMENT");

            if (null != retMap) {
                //向统一支付平台发起请求
                String json = HttpUtils.postsJson(PAY_URL + "/alischool/groupkeyQuery", retMap);
                log.info(json);
                JSONObject results = JSON.parseObject(json);
                if (results.getInteger("code") == 200) {
                    results = JSON.parseObject(results.getString("data"));

                    responseMap.put("schoolStdCode", results.getString("schoolStdCode"));
                    responseMap.put("schoolStdOutCode", results.getString("schoolStdOutCode"));

                } else {
                    return ResultUtil.error(ResultEnum.Err_4118);
                }
            } else {
                return ResultUtil.error(ResultEnum.Err_4118);
            }
        }

        return ResultUtil.success(responseMap);
    }


}